package assetservicelogic

import (
	"context"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/BeanUtils"
	"property_service/utils/result"

	"github.com/zeromicro/go-zero/core/logx"
)

type AssetDepreciationAddListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAssetDepreciationAddListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AssetDepreciationAddListLogic {
	return &AssetDepreciationAddListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// AssetDepreciationAddList 批量创建折旧管理
func (l *AssetDepreciationAddListLogic) AssetDepreciationAddList(in *property_service.AssetDepreciationList) (*property_service.Response, error) {
	//循环将结构体转为&model.AssetDepreciation
	var list []*model.AssetDepreciation
	for _, v := range in.List {
		mod := &model.AssetDepreciation{
			AssetID:                  v.AssetID,                               // 资产ID
			AssetCode:                v.AssetCode,                             // 资产编码
			CurrentMonthDepreciation: v.CurrentMonthDepreciation,              // 本月折旧
			CardNumber:               v.CardNumber,                            // 卡片编号
			AssetName:                v.AssetName,                             // 资产名称
			AssetCategory:            v.AssetCategory,                         // 资产分类
			AssetCategoryID:          v.AssetCategoryID,                       // 资产分类ID
			DepreciationPeriod:       v.DepreciationPeriod,                    // 折旧期间
			DataIdentifier:           v.DataIdentifier,                        // 数据标识
			Warehouse:                v.Warehouse,                             // 库房
			UsingDept:                v.UsingDept,                             // 使用科室
			UsingDeptID:              v.UsingDeptID,                           // 使用科室ID
			OriginalValue:            v.OriginalValue,                         // 资产原值
			AccumulatedDepreciation:  v.AccumulatedDepreciation,               // 累计折旧额
			Depreciation:             v.Depreciation,                          //月折旧额
			DepreciationRate:         v.DepreciationRate,                      //月折旧率
			DepreciationRateYear:     v.DepreciationRateYear,                  //年折旧率
			DepreciationMethodID:     v.DepreciationMethodID,                  // 折旧方法ID
			DepreciationMethod:       v.DepreciationMethod,                    // 折旧方法
			PurchaseDate:             BeanUtils.GetDateFormat(v.PurchaseDate), // 购置日期
			UsefulLife:               v.UsefulLife,                            // 使用年限
			ExpectedUsefulLife:       v.ExpectedUsefulLife,                    // 预计使用年限
			DepreciatedMonths:        v.DepreciatedMonths,                     // 已折旧月数
			ResidualValue:            v.ResidualValue,                         // 残值
			Residual:                 v.Residual,                              //残值率
			StatusID:                 v.StatusID,                              // 状态ID
			StatusName:               v.StatusName,                            // 状态名称
			Remarks:                  v.Remarks,                               // 备注
			DepreciationStatus:       v.DepreciationStatus,                    // 折旧状态
			StockID:                  v.StockID,                               // 仓库ID
			TenantID:                 v.TenantID,                              // 租户ID
			CreatedAtuser:            v.CreatedAtuser,                         // 创建人
			UpdatedAtuser:            v.UpdatedAtuser,                         // 更新人
		}
		list = append(list, mod)
	}
	//调用批量新增方法
	// 获取折旧管理表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).AssetDepreciation
	//返回结果
	// 使用上下文执行创建操作
	//批量新增，每次添加100条数据
	if err := m.WithContext(l.ctx).CreateInBatches(list, 100); err != nil {
		// 如果创建失败，返回错误响应
		return result.Err(err).Ed()
	}
	// 创建成功，返回成功响应
	return result.Suc().AddEd()
}
